// miniprogram/pages/reply/reply.js
const db = wx.cloud.database();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    comments: []
  },

  getComments: function () {
    wx.showLoading({
      title: '加载中',
    })
    db.collection("comments").where({
      toId: app.globalData.openid
    }).orderBy('createTime', 'desc').get().then(res => {
      this.setData({
        comments: res.data
      })
    }).then(res => {
      wx.hideLoading()
    })
  },

  onComment: function (event) {
    //console.log(event)
    wx.showModal({
      title: '回复 or 私聊？',
      confirmText: '回复',
      cancelText: '私聊',
      cancelColor: 'cancelColor',
      success: res => {
        if (res.confirm) {
          wx.navigateTo({
            url: '../movementInfo/movementInfo?id=' + event.currentTarget.dataset.id + '&to=' + event.currentTarget.dataset.openid,
            events: {
              id: event.currentTarget.dataset.id,
              to: event.currentTarget.dataset.openid
            }
          })
        } else {
          if (event.currentTarget.dataset.anonymous) {
            //console.log(event.currentTarget.dataset.name)
            wx.navigateTo({
              url: '../anonymousChat/index?id=' + event.currentTarget.dataset.openid + '&name=' + event.currentTarget.dataset.name,
            })
          }else{
            //console.log(event.currentTarget.dataset.name)
            wx.navigateTo({
              url: '../privateChat/index?id=' + event.currentTarget.dataset.openid + '&name=' + event.currentTarget.dataset.name,
            })
          }
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getComments()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})